﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.DAO.基础;
using LibrarySolution.ComMethod;
using LibrarySolution.DAO.委托;
using LibrarySolution.Model.基础;
using LibrarySolution.Frm;
using LibrarySolution.Frm.报表;
using Microsoft.Reporting.WinForms;

namespace LibrarySolution.Frm
{
    public partial class FrmBkxx : Form
    {
        #region 属性
        Delegates delegates = new Delegates();
        BkxxDao bkxxDao = new BkxxDao();
        private static FrmBkxx Instance;
        #endregion

        #region 构造器
        public FrmBkxx()
        {
            InitializeComponent();
            delegates.refreshLBEvent += new Delegates.refreshLBEventHandler(delegates_refreshLBEvent);
            if (GlobalVar.NoGly == false && GlobalVar.SpGly == false)
            { toolStrip1.Items["TstripBtnAdd"].Visible = false;
            toolStrip1.Items["TstripBtnDel"].Visible = false;
            }
            refreshDtgv();
        }
        #endregion

        #region 事件
        public static FrmBkxx GetInstance()
        {
            if (Instance == null || Instance.IsDisposed)
            {
                Instance = new FrmBkxx();
                Instance.MdiParent = GlobalVar.frmMain;
                GlobalVar.frmMain.panel1.Controls.Add(Instance);
            }
            if (GlobalVar.YhEnter == true)
            {
                Instance.toolStrip1.Items[0].Visible = false;
                Instance.toolStrip1.Items[1].Visible = false;
                Instance.toolStrip1.Items[3].Visible = false;
                Instance.dataGridView1.Columns["用户姓名"].Visible = false;
                Instance.dataGridView1.Columns["书本编码"].Visible = false;
                Instance.dataGridView1.Columns["入库时间"].Visible = false;
            }
            Instance.BringToFront();
            return Instance;
        }

        private void TxtQueryByBkName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                BkxxDetail bkxxDetail = new BkxxDetail();
                bkxxDetail.bk_name = TxtQueryByBkName.Text.Trim();
                DataTable dt = null;
                dt = bkxxDao.getDTbyBkName(bkxxDetail);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或书本不存在!");
                else
                {
                    dataGridView1.DataSource = dt;
                    dataGridView1.Refresh();
                }
                TxtQueryByBkName.Text = "";
            }
        }

        private void TxtQueryByWriter_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar== 13)
            {
                DataTable dt = null;
                BkxxDetail bkxxDetail = new BkxxDetail();
                bkxxDetail.bk_writer = TxtQueryByWriter.Text.Trim();
                dt = bkxxDao.getDtbyWriter(bkxxDetail);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或书本不存在!");
                else
                {
                    dataGridView1.DataSource = dt;
                    dataGridView1.Refresh();
                }
                TxtQueryByWriter.Text = "";
            }
        }

        private void TxtQueryByNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                DataTable dt = null;
                BkxxDetail bkxxDetail = new BkxxDetail();
                bkxxDetail.bk_no = TxtQueryByNo.Text.Trim();
                dt = bkxxDao.getDtbyBkNo(bkxxDetail);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或书本不存在");
                else
                {
                    dataGridView1.DataSource = dt;
                    dataGridView1.Refresh();
                }
                TxtQueryByNo.Text = "";
            }
        }

        private void TstripBtnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除选中项!","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                string bkNo = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
                bkxxDao.DelSelectRow(bkNo);
                refreshDtgv();
            }
        }

        private void TstripBtnRef_Click(object sender, EventArgs e)
        {
            refreshDtgv();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            String bkNo;
            BkxxDetail bkxxDetail = new BkxxDetail();
            bkNo = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
            bkxxDetail.bk_no = bkNo;
            bkxxDetail = bkxxDao.getBkxxDetialByNo(bkxxDetail);
            GlobalVar.BkxxDetail = bkxxDetail;
            FrmBkxxModify.IsModify = true;
            FrmBkxxModify.GetInstance(delegates).ShowDialog();
        }

        private void TstripBtnAdd_Click(object sender, EventArgs e)
        {
            FrmBkxxModify.IsModify = false;
            FrmBkxxModify.GetInstance(delegates).ShowDialog();
        }

        private void TstripBb_Click(object sender, EventArgs e)
        {
            String time = DataUtil.getTime();
            BkxxDetail bkxxDetail = new BkxxDetail();
            String path = GlobalVar.path + @"\Frm\报表\BkReport.rdlc";
            ReportDataSource Rds = new ReportDataSource();
            Rds.Name = "DataSet";
            Rds.Value = bkxxDao.getBkxxBb(bkxxDetail);
            FrmBb.GetInstance(Rds, path, time).ShowDialog();
        }
        #endregion

        #region 方法
        void delegates_refreshLBEvent()
        {
            refreshDtgv();
        }

        void refreshDtgv()
        {
            BkxxDetail bkxxDetail = new BkxxDetail();
            DataTable dt = null;
            dt = bkxxDao.getDataTable(bkxxDetail);
            groupBox4.Text = "馆藏图书共:" + bkxxDao.getBkCount() + "本";
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[10].Visible =false;
            dataGridView1.Refresh();
        }
        #endregion


    }
}
